#include <iostream>
#include <cstdio>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>


// enum {
//     A = -1
// };


// int main()
// {

//     FILE *fp = fopen("./log.txt", "r");
//     if (fp == nullptr)
//     {
//         return A;
//     }
//     return 0;
// }


const std::string myenv = "HELLO=AAAAAAAAAAAAAAA";


extern char **environ;
//由子进程执行程序
int main() 
{
    putenv((char*)myenv.c_str());
    pid_t id = fork();
    if (id == 0)
    {
        char *const argv[] = {
            (char *)"other",
            // (char *)"ls",//因为是字符串常量，强转一下避免警告
            // (char *)"--color",
            // (char *)"-a",
            // (char *)"-l",
            nullptr
        };
        char * const env[] = {
            (char*)"HELLO=bite",
            (char*)"HELLO1=bite1",
            (char*)"HELLO2=bite2",
            (char*)"HELLO3=bite3",
            nullptr
        };

        execvpe("./other",argv,environ);

        //execl("./other","other",nullptr);

        //execvp(argv[0],argv);
        //execlp("ls","ls","--color","-a","-n","-l",nullptr);
        //execv("/usr/bin/ls",argv);
        // child
        //execl("/bin/ls", "ls","-1","-a","-n","--color", nullptr);
        //走到这一定是失败的
        exit(1);
    }
    pid_t rid = waitpid(id,nullptr,0);
    if(rid > 0)
    {
        printf("等待子进程成功!\n");
    }

    // printf("我是myexec,pid: %d\n", getpid());
    // // execl("./other","other",nullptr);

    // // int n = execl("/bin/ls","ls","-1","-a","-n","--color",nullptr);
    // int n = execl("./myexec", "myexec", nullptr);

    // printf("execl return val: %d\n", n);
    // // execl("/usr/bin/top","top",nullptr);
    // printf("hello");
    return 0;
}